package com.dldata.devtools30.controller.metadata;

import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.common.EnumUtils;
import com.dldata.devtools30.controller.DevBase30Controller;
import com.dldata.devtools30.entity.DevtModelMetadataFieldEntity;
import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.repository.common.DlBaseJdbcTemplate;
import com.dldata.devtools30.service.metadata.DevtModelMetadataFieldService;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTableService;
import com.dldata.devtools30.utils.StringUtil;
import com.dldata.devtools30.utils.Util;
import com.dldata.devtools30.utils.dbtable.DbTableManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/V_DevtModelMetadataFieldController")
public class V_DevtModelMetadataFieldController extends DevBase30Controller {

    @Autowired
    DevtModelMetadataFieldService metadataFieldService;

    @Autowired
    DevtModelMetadataTableService metadataTableService;

    @Autowired
    DlBaseJdbcTemplate dlBaseJdbcTemplate;

    @RequestMapping("edit")
    public String edit(String id, Model model, String formId){
        id = StringUtil.isEmptyOrLength0(id)? UUID.randomUUID().toString():id;
        List<DevtModelMetadataFieldEntity> metadataFieldEntities= metadataFieldService.findById(id);
        if(metadataFieldEntities.size()>0){
            model.addAttribute("data",metadataFieldEntities.get(0));
            model.addAttribute("formId",metadataFieldEntities.get(0).getFormId());
            if(metadataFieldEntities.get(0).getPkey()==null){
                metadataFieldEntities.get(0).setPkey(0);
            }
        }else{
            DevtModelMetadataFieldEntity metadataFieldEntity=new DevtModelMetadataFieldEntity();
            metadataFieldEntity.setId("");
            metadataFieldEntity.setPkey(0);
            metadataFieldEntity.setTypeLength(255);
            metadataFieldEntity.setTypeZeroLength(0);
            List<DevtModelMetadataFieldEntity> allFormField = metadataFieldService.getAllByTid(formId);
            int xh = allFormField.size() + 1;
            for (int i = 0; i < allFormField.size(); i++) {
                DevtModelMetadataFieldEntity fieldEntity = allFormField.get(i);
                xh=fieldEntity.getXh()>=xh?(fieldEntity.getXh()+1):xh;
            }
            metadataFieldEntity.setXh(xh);
            model.addAttribute("data", metadataFieldEntity);
            model.addAttribute("formId",formId);
        }

        model.addAttribute("fieldtypesList",Util.getEnumMapNative(EnumUtils.FIELDTYPE.class));

        List<DevtModelMetadataTableEntity> metadataTableEntities = commonApiDao.getScrollData(DevtModelMetadataTableEntity.class).getResultlist();
        model.addAttribute("metadataTableEntities",metadataTableEntities);

        return "thymeleaf/devtools30/metadata/v_MetadataFieldEdit";
    }
    
    
    @RequestMapping("toFieldEditFromDb")
    public String toSysFormFieldEditFromDb(Model model,String formId){
        DevtModelMetadataTableEntity metadataTableEntity=metadataTableService.findOne(formId);
        
        DbTableManager dbTableManager=new DbTableManager(dlBaseJdbcTemplate);
        List<DevtModelMetadataFieldEntity>  fieldEntities=dbTableManager.queryTableFields(metadataTableEntity);
        model.addAttribute("fieldEntities", JSON.toJSONString(fieldEntities));
        model.addAttribute("formId",formId);
        model.addAttribute("fieldtypesList",Util.getEnumMapNative(DbTableManager.FIELDTYPE.class));
        return "thymeleaf/devtools30/metadata/v_MetadataFieldEditFromDb";
    }
}
